# -*- coding:utf-8 -*-

# @File     :asset
# @Author   :Yupeng
# @Date     :2019/2/26 15:30

'''
用于图像识别，表示用户上传的图片资源
'''
from enum import Enum
import uuid
import os
import hashlib
from datetime import datetime

from django.db import models
from django.conf import settings
from django.contrib.postgres.fields import ArrayField

from apps.accounts.models import User
from .task import VisualRecognitionTask


def unpressed_directory_path(instance, filename):
    # ext = filename.split('.')[-1]
    # filename = '{}.{}'.format(uuid.uuid4().hex[:10], ext)
    return '{0}/{1}/{2}'.format('images', instance.father_path, filename)


class UploadMethod(Enum):
    '''
    上传方式的枚举
    '''
    pass


class Asset(models.Model):
    '''
    单纯的用于记录上传的文件，包括压缩包、图片，全部统一记录.这样方便个人资料里面展示
    father_path:这些文件所在文件夹的路径
    '''
    # father_path = models.CharField(max_length=254, null=True, default='all')
    upload_time = models.DateTimeField(auto_now_add=True)
    # file = models.FileField(upload_to=unpressed_directory_path)
    file = models.FileField(upload_to='images/')
    author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='user_asset')
    hash_value = models.CharField(max_length=254, null=True)

    class Meta:
        ordering = ('-upload_time',)

    def to_dict(self):
        '''

        :return:
        '''
        d = {
            'id': self.id,
            'upload_time': self.upload_time,
            'path': str(self.file),
            'author': self.author.username
        }
        return d

    @property
    def filename(self):
        return os.path.basename(self.file.name)

    @property
    def image_path(self):
        return str(self.file)


class ClassAsset(models.Model):
    '''
    某个分类任务中用户创建的分类对应的图片资源,是按照集合或包的形式存在的
    package_path:用户上传的是压缩包路径
    path:图片文件夹路径、如果用户上传的是压缩包，则图片文件夹与压缩包同级目录
    '''
    title = models.CharField(max_length=254)
    author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='user_classasset')
    tag = models.CharField(max_length=254, null=True)
    description = models.CharField(max_length=254, null=True, blank=True)
    num_of_pictures = models.IntegerField(default=0)
    created_time = models.DateTimeField(auto_now_add=True)
    update_time = models.DateTimeField(auto_now=True)
    # package = models.FileField(upload_to=package_directory_path, null=True)
    # path = models.FilePathField(null=True)
    images_path = ArrayField(models.CharField(max_length=254), null=True)
    vrtask = models.ForeignKey(VisualRecognitionTask, null=True, on_delete=models.CASCADE, related_name='vrtask_classasset')

    class Meta:
        ordering = ('-update_time',)

    def get_abs_asset_path(self):
        return os.path.join(settings.MEDIA_ROOT, str(self.path))

    # @property
    # def images_path(self):
    #     return list(self.images_path)

    def to_dict(self):
        '''
        返回字段dict
        :return:
        '''
        d = {
            'id': self.id,
            'class_title': self.title,
            'tag': self.tag,
            'num_of_pictures': self.num_of_pictures,
            'description': self.description,
            'images_path': self.images_path
        }
        return d
